<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="Access-Control-Allow-Origin" content="ttp://127.0.0.1:5500">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../css/public.css" media="all">
</head>
<body>

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" id="from1">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">医生姓名</label>
                            <div class="layui-input-inline">
                                <input type="text" name="doctorName" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">患者姓名</label>
                            <div class="layui-input-inline">
                                <input type="text" name="username" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">患者病症</label>
                            <div class="layui-input-inline">
                                <input type="text" name="disease" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">医生id</label>
                            <div class="layui-input-inline">
                                <input type="text" name="doctorId" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-sm" lay-event="getCheckData">获取选中行数据</button>
                <button class="layui-btn layui-btn-sm" lay-event="getCheckLength">获取选中数目</button>
                <button class="layui-btn layui-btn-sm" lay-event="isAll">验证是否全选</button>
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加 </button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 删除 </button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="bar"> 医生比例柱状图 </button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="pie">医生比例饼状图 </button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="export">导出excel </button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="import">导入excel </button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="download">excel模版下载 </button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="select">查看药单</a>
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>

<script src="../layui/layui.js" charset="utf-8"></script>
</body>
<script>
    layui.use(['form', 'table'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;

            $.ajaxSetup({
            // 发送cookie
            xhrFields: {
                withCredentials: true
            }
        })

        table.render({
            elem: '#currentTableId',
            url: 'http://localhost:8080/demo/prescription/selectPrescriptionLike',
            toolbar: '#toolbarDemo',
            title:'处方列表',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {type: "checkbox", width: 50},
                {field: 'id', title: 'ID', width: 70, unresize: true, sort: true },
                {field: 'doctorId', width: 90, title: '医生id',sort: true },
                {field: 'doctorName', width: 100, title: '医生名称'},
                {field: 'userId', width: 90, title: '患者id', sort: true},
                {field: 'userName', width: 100, title: '患者名称'},
                {field: 'disease', width: 100, title: '患者病症'},
                {field: 'description', width:120, title: '病症描述'},
                {field: 'totalPrice', width: 120, title: '药品总价', sort: true},
                {field: 'payType', width: 110, title: '支付方式', sort: true},
                {field: 'payStatus', width: 110, title: '支付状态', sort: true},
                {field: 'createDate', width: 110, title: '建档时间', sort: true},
                {field: 'url', width: 110, title: '二维码', sort: true},
                {title: '操作', minWidth: 200, toolbar: '#currentTableBar', align: "center"}
            ]],
            page: true,
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            var result = JSON.stringify(data.field);
            layer.alert(result, {
                title: '最终的搜索信息'
            });

            //执行搜索重载
            table.reload('currentTableId', {
                // url: "http://localhost:8080/demo/prescription/selectPrescriptionLike",
                page: {
                    curr: 1
                }
                , where: {
                    //searchParams: result
                    "doctorName": data.field.doctorName,
                    "suserNameex": data.field.userName,
                    "disease": data.field.disease,
                    "doctorId": data.field.doctorId
                }
            }, true);

            return false;  // 如果 改为 true   ，表格会 再查询一次 ,所以 这里写为 false
        });



        //头工具栏事件
        table.on('toolbar(currentTableFilter)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            switch (obj.event) {
                case 'getCheckData':
                    // 得到 选中行对象
                    var data = checkStatus.data;
                    layer.alert(JSON.stringify(data));
                    break;
                case 'getCheckLength':
                    var data = checkStatus.data;
                    layer.msg('选中了：' + data.length + ' 个');
                    break;
                case 'isAll':
                    layer.msg(checkStatus.isAll ? '全选' : '未全选');
                    break;

                case 'add':
                    layer.open({
                        title: '添加学员',
                        shade: 0.2,
                        type: 2,
                        shadeClose: true,   // 点别的地方 是否关闭当前 layer    true收关闭   false 不关闭
                        area: ['80%', '100%'],
                        fixed: true, //不固定
                        maxmin: true,
                        content: 'prescriptionAdd.html',
                        end: function (index) {
                            layer.msg("添加成功");
                            table.reload('currentTableId');
                        }
                    });
                    break;
                case 'delete':
                    layer.msg('delete');
                    break;
                case 'bar':
                   layer.open({
                       title:'医生比例',
                       shade:0.2,
                       type:2,
                       shadeClose:true,
                       area:['80%','100%'],
                       fixed:true,
                       maxmin:true,
                       content:'bar.html',
                       end:function (index) {

                       }
                   });
                    break;
                case 'pie':
                    layer.open({
                        title:'医生比例',
                        shade:0.2,
                        type:2,
                        shadeClose:true,
                        area:['80%','100%'],
                        fixed:true,
                        maxmin:true,
                        content:'pie.html',
                        end:function (index) {

                        }
                    });
                    break;
                case 'export':
                    location.href="http://localhost:8080/demo/prescription/export?"+$("#from1").serialize();
                    break;   
                    
                case 'import':
                    layer.open({
                            title: '学员信息导入',
                            shade: 0.2,
                            type: 2,
                            shadeClose: true,   // 点别的地方 是否关闭当前 layer    true收关闭   false 不关闭
                            area: ['50%', '80%'],
                            fixed: true, //不固定
                            maxmin: true,
                            content: './prescriptionimport.html',
                            end: function (index) {
                                table.reload('studentTable');
                            }
                        });
                    break;
  
                case 'download':
                    location.href='http://localhost:8080/demo/prescription/download';
                    break;
                //自定义头工具栏右侧图标 - 提示
                case 'LAYTABLE_TIPS':
                    layer.alert('这是工具栏右侧自定义的一个图标按钮');
                    break;
            };
        });


        /**
         * toolbar监听事件
         */
        // table.on('toolbar(currentTableFilter)', function (obj) {
        //     if (obj.event === 'add') {  // 监听添加操作
        //         var index = layer.open({
        //             title: '添加用户',
        //             type: 2,
        //             shade: 0.2,
        //             maxmin:true,
        //             shadeClose: true,
        //             area: ['100%', '100%'],
        //             content: 'prescriptionAdd.html',
        //         });
        //         $(window).on("resize", function () {
        //             layer.full(index);
        //         });
        //     } else if (obj.event === 'delete') {  // 监听删除操作
        //         var checkStatus = table.checkStatus('currentTableId')
        //             , data = checkStatus.data;
        //         layer.alert(JSON.stringify(data));
        //     }
        // });

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data; //获得当前行数据
            //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var layEvent = obj.event;
            var tr = obj.tr; //获得当前行 tr 的 DOM 对象（如果有的话）
            localStorage.setItem("PrescriptionItem", JSON.stringify(data));

            if (layEvent === 'select') {
                layer.open({
                    title: '查看药单',
                    shade: 0.1,
                    type: 2,
                    shadeClose: true,   // 点别的地方 是否关闭当前 layer    true收关闭   false 不关闭
                    area: ['80%', '100%'],
                    fixed: true, //不固定
                    maxmin: true,
                    content: 'itemList.html',
                    end: function (index) {
                        table.reload('currentTableId');
                    }
                });
                
            } else if (layEvent === 'edit') {
                layer.open({
                    title: '编辑',
                    shade: 0.1,
                    type: 2,
                    shadeClose: true,   // 点别的地方 是否关闭当前 layer    true收关闭   false 不关闭
                    area: ['80%', '100%'],
                    fixed: true, //不固定
                    maxmin: true,
                    content: 'prescriptionEdit.html',
                    end: function (index) {
                        layer.msg("修改成功");
                        table.reload('currentTableId');
                    }
                });
              
            } else if (layEvent === 'delete') {
                layer.confirm('确定删除', function(index){
                    // 向服务端发送删除请求
                    // 此处可以发送ajax
                    $.get("http://localhost:8080/demo/prescription/deletePrescriptionById",{"id":data.id},function (result){
                        if (result.code == 0) {
                            layer.msg("删除成功");
                            obj.del(); //删除对应行（tr）的DOM结构
                        } else {
                            layer.msg("删除失败");
                        }
                    },"json");
                    layer.close(index);
                });
            }
        });

    });
</script>
</html>